package com.canbot.u05.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

import com.canbot.u05.adapter.PhotoItem.onCheckBoxCheckedListener;
import com.canbot.u05.adapter.PhotoItem.onGridViewItemClickListener;
import com.canbot.u05.bean.AudioInfo;
import com.canbot.u05.utils.Logger;

import java.util.ArrayList;
import java.util.List;

public class AudioGridViewAdapter extends BaseAdapter {

        private Context context;

        private List<AudioInfo> data;

        private LayoutInflater inflater;

        private onCheckBoxCheckedListener onBoxCheckedListener;

        public AudioGridViewAdapter(Context context, List<AudioInfo> data,
                                    onCheckBoxCheckedListener onBoxCheckedListener,
                                    onGridViewItemClickListener onGridViewItemClickListener) {
                super();
                this.context = context;
                setData(data);
                this.onBoxCheckedListener = onBoxCheckedListener;
                inflater = LayoutInflater.from(context);

        }

        private void setData(List<AudioInfo> data) {
                if (data == null) {
                        data = new ArrayList<AudioInfo>();
                }
                this.data = data;
        }

        @Override
        public int getCount() {
                return data.size();
        }

        @Override
        public AudioInfo getItem(int position) {
                return data.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                PhotoItem item = null;
                if (convertView == null || !(convertView instanceof PhotoItem)) {
                        item = new PhotoItem(context, onBoxCheckedListener);
                        convertView = item;
                }
                else {
                        item = (PhotoItem) convertView;
                }
                item.setMimeType(data.get(position));
                item.setSelected(data.get(position).isChecked());
                final CheckBox box = item.getCheckBox();
                if (data.get(position).isChecked()) {
                        box.setChecked(true);
                }
                item.setFocusable(false);
                item.setFocusableInTouchMode(false);
                Logger.e("t","音频文件="+data.get(position).toString());
                String[] str = data.get(position).getPath().split("/");
                item.setNum(str[str.length-1]);
//                int i = (position + 1) / 10;
//                if (i == 0) {
//                        int p = position + 1;
//                        String num = "00" + p;
//                        item.setNum(num);
//                }
//                else if (i > 0 && i < 10) {
//                        int p = position + 1;
//                        String num = "0" + p;
//                        item.setNum(num);
//                }
//                else {
//                        item.setNum(position + 1 + "");
//                }
                return convertView;
        }


}
